<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="titleOrg">
        <el-input
          v-model="queryParams.titleOrg"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息分类" prop="infoType">
        <el-select v-model="queryParams.infoType" placeholder="请选择信息分类" clearable size="small">
          <el-option
            v-for="dict in dict.type.disaster_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:dailyMonitoring:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:dailyMonitoring:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['information:dailyMonitoring:export']"
        >导出所选</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleAllExport"
          v-hasPermi="['information:dailyMonitoring:export']"
        >导出全部</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyMonitoringList" @selection-change="handleSelectionChange" ref="multipleTable" @select="handleSelect" @select-all="handleSelectAll">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column width="80" label="信息分类" align="center" prop="infoType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.disaster_type" :value="scope.row.infoType"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="titleFor" >
        <template slot-scope="scope">
          <a :href=scope.row.link target="_blank" :title=scope.row.titleFor><span v-html="scope.row.titleFor"></span></a>
        </template>
      </el-table-column>
      <el-table-column label="摘要" prop="abstractFor" >
        <template slot-scope="scope">
          <p v-html = 'scope.row.abstractFor'></p>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" align="center" prop="publishFor" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishFor, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:dailyMonitoring:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:dailyMonitoring:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改每日监测信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原始标题" prop="titleOrg">
          <el-input v-model="form.titleOrg" placeholder="请输入原始标题" />
        </el-form-item>
        <el-form-item label="规范标题" prop="titleFor">
          <el-input v-model="form.titleFor" placeholder="请输入规范标题" />
        </el-form-item>
        <el-form-item label="原始摘要" prop="abstractOrg">
          <el-input v-model="form.abstractOrg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="规范摘要" prop="abstractFor">
          <el-input v-model="form.abstractFor" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="原始发布日期" prop="publishOrg">
          <el-input v-model="form.publishOrg" placeholder="请输入原始发布日期" />
        </el-form-item>
        <el-form-item label="规范发布日期" prop="publishFor">
          <el-date-picker clearable size="small"
            v-model="form.publishFor"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择规范发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="信息分类" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择信息分类">
            <el-option
              v-for="dict in dict.type.disaster_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDailyMonitoring, getDailyMonitoring, delDailyMonitoring, addDailyMonitoring, updateDailyMonitoring, exportDailyMonitoring, exportAllDailyMonitoring } from "@/api/information/dailyMonitoring";
import {exportArticle} from "@/api/information/article";

export default {
  name: "DailyMonitoring",
  dicts: ['disaster_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 每日监测信息表格数据
      dailyMonitoringList: [],
      multipleSelection:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 信息分类时间范围
      daterangePublishFor: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        titleOrg: null,
        titleFor: null,
        infoType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询每日监测信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePublishFor && '' != this.daterangePublishFor) {
        this.queryParams.params["beginPublishFor"] = this.daterangePublishFor[0];
        this.queryParams.params["endPublishFor"] = this.daterangePublishFor[1];
      }
      listDailyMonitoring(this.queryParams).then(response => {
        this.dailyMonitoringList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        titleOrg: null,
        titleFor: null,
        abstractOrg: null,
        abstractFor: null,
        publishOrg: null,
        publishFor: null,
        infoType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePublishFor = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加每日监测信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDailyMonitoring(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改每日监测信息";
      });
    },
    // 全选按钮同步选中信息
    handleSelectAll (selection) {
      this.articleList.map(row => {
        const index = this.multipleSelection.findIndex(e => e.id === row.id)
        // 全选，将选项加入 multipleSelection
        if (selection.length && index === -1) {
          this.multipleSelection.push(row)
        }
        // 取消全选，将选项从 multipleSelection 中移除
        if (!selection.length && index > -1) {
          this.multipleSelection.splice(index, 1)
        }
      });
    },
    // 单行选中按钮同步选中信息
    handleSelect (selection, row) {
      // 同步选中的数据到 multipleSelection
      const index = this.multipleSelection.findIndex(e => e.id === row.id)
      if (index > -1) {
        this.multipleSelection.splice(index, 1)
      } else {
        this.multipleSelection.push(row)
      }
    },
    handleCollection(){
      this.collectionOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDailyMonitoring(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDailyMonitoring(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除每日监测信息编号为"' + ids + '"的数据项？').then(function() {
        return delDailyMonitoring(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const ids = [];
      this.multipleSelection.forEach(function(item, index, obj){
        ids.push(item.id);
      })
      this.$modal.confirm('是否导出所选项？').then(() => {
        this.exportLoading = true;
        return exportDailyMonitoring(ids);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleAllExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有每日监测信息数据项？').then(() => {
        this.exportLoading = true;
        return exportAllDailyMonitoring(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
